package com.eascs.admin.o2o.badloan.controller;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.admin.o2o.badloan.entity.O2oCacleForm;
import com.eascs.admin.o2o.badloan.service.O2oCancleServiceImpl;
import com.eascs.app.dao.PageResult;
import com.eascs.common.doc.excel.DefaultRowWriteHandler;
import com.eascs.common.doc.excel.ExcelApiHelper;
import com.eascs.common.exception.RuntimeIOException;
import com.eascs.common.util.IoUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.paymentdetail.controller.O2oPaymentDetailController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.tag.MenuTag;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.service.sys.SysOrgService;

@Controller
@RequestMapping("/cancel")
public class CancelQueryController extends BaseMasterViewController {



	@Autowired
	O2oCancleServiceImpl o2oCancleServiceImpl;

	@Reference
	SysOrgService sysOrgService;
	


	
	/**
	 * @Title: list
	 * @Description: 作废贷款查询
	 * @author 张庆奇
	 * @param vo
	 * @return
	 * @return String
	 * @throws
	 * @date 2017年8月30日 上午10:15:50
	 */
	@RequestMapping("/list")
	public String list(O2oCacleForm  vo) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "CancelQuery");
		vo.setPageSize(10);
		List<ActOrgEntity> orgProvince = sysOrgService.getOrgProvince();
		ViewContext.put("ActOrgEntitys", orgProvince);
		String o2ourl = "/cancel/list";
		String reporto2ourl = "/cancel/download";
		String loanType = vo.getLoanType() == null ? "" : vo.getLoanType();
		String billCode = vo.getBillCode() == null ? "" : vo.getBillCode();
		String userSurname=vo.getUserSurname()==null?"":vo.getUserSurname();
        String operatorName=vo.getOperatorName()==null?"":vo.getOperatorName();//信贷经
		String loadName=vo.getLoadName()==null?"":vo.getLoadName();//经营平台
		String hostProvince = vo.getHostProvince() == null ? "" : vo.getHostProvince();
		String startTime = vo.getStartTime()== null ? "" : vo.getStartTime();
		String endTime = vo.getEndTime() == null ? "" : vo.getEndTime();
		o2ourl = UrlUtil.appendUrl(o2ourl, "loanType", loanType);
		o2ourl = UrlUtil.appendUrl(o2ourl, "billCode", billCode);
		o2ourl = UrlUtil.appendUrl(o2ourl, "userSurname", userSurname);
		o2ourl = UrlUtil.appendUrl(o2ourl, "operatorName", operatorName);
		o2ourl = UrlUtil.appendUrl(o2ourl, "loadName", loadName);
		o2ourl = UrlUtil.appendUrl(o2ourl, "hostProvince", hostProvince);
		o2ourl = UrlUtil.appendUrl(o2ourl, "startTime", startTime);
		o2ourl = UrlUtil.appendUrl(o2ourl, "endTime", endTime);
		o2ourl = o2ourl + "&pageIndex=";
		reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "loanType", loanType);
		reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "billCode", billCode);
		reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "userSurname", userSurname);
		reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "operatorName", operatorName);
		reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "loadName", loadName);
		reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "hostProvince", hostProvince);
		reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "startTime", startTime);
		reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "endTime", endTime);
		PageResult<Object[]> pageResult =o2oCancleServiceImpl.findCancelLoanByPage(vo) ;
		String titles[] = { "贷款单号", "贷款类型", "借款人", "信贷经理", "经营平台", "省区","创建时间", "作废人", "作废时间","作废流程单号" };// ,
		SelectDataOption[] selectDataOptions = {null, new SelectDataOption("OTO_LOAN_TYPE"), null, null, null, null, null, null, null,null};
		Button[] titleButtons = new Button[] {null,null,null,null,null,null,null,null,null, ButtonHelper.createAjaxButton(null, "/cacler/detail?id={11}") };
		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
		o2oPagingInfo.setFirstRenderColumnIndex(0);
		o2oPagingInfo.setTitleButtons(titleButtons);
		ViewContext.put("paginationInfo", o2oPagingInfo);
		ViewContext.put("vo", vo);
		ViewContext.put("reporto2ourl", reporto2ourl);
		return "/cancel/list";
	}
	
	@RequestMapping("/download")
	public void dowanload(O2oCacleForm vo, HttpServletResponse httpServletResponse, HttpServletRequest request) {
		vo.setPageSize(80000);
		PageResult<Object[]> pageResult = o2oCancleServiceImpl.findCancelLoanByPage(vo);
		String path = O2oPaymentDetailController.class.getResource("/excelTemplate").getPath();
		StringBuffer s = new StringBuffer();
		String outPath = s.append(path).append("/cancelLoan-model.xls").toString();
		OutputStream os = null;
		try {
			String outfile = "作废贷款明细数据.xls";
			String userAgent = request.getHeader("User-Agent");
			byte[] bytes = userAgent.contains("MSIE") ? outfile.getBytes() : outfile.getBytes("UTF-8");
			outfile = new String(bytes, "ISO-8859-1"); // 各浏览器基本都支持ISO编码
			File f = new File(outPath);
			if (!f.exists())
				throw new RuntimeIOException("file not found");
			os = httpServletResponse.getOutputStream();
			httpServletResponse.reset();
			httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + outfile);
			httpServletResponse.setContentType("application/octet-stream; charset=utf-8");
			DefaultRowWriteHandler defaultRowWriteHandler = new DefaultRowWriteHandler(pageResult.getData());
			ExcelApiHelper.writeHandle(httpServletResponse.getOutputStream(), O2oPaymentDetailController.class.getResourceAsStream("/excelTemplate/cancelLoan-model.xls"), defaultRowWriteHandler);
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
			IoUtil.fileToOutputStream(f, byteArrayOutputStream);
			os.write(byteArrayOutputStream.toByteArray());
			os.flush();
			os.close();
		} catch (Exception e) {
			throw new RuntimeIOException(e);
		} finally {
			IoUtil.close(os);
		}
	}


}
